package game;

import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class Menu {
    protected GrandeCanvas canvas;
    protected ArrayList<MenuItem> elements;
    protected Menu subMenu;
    protected Menu parent;

    public Menu(GrandeCanvas c){
        canvas = c;
        elements = new ArrayList<MenuItem>();
        parent = null;
    }
    
    public Menu(Menu parent){
        this.parent = parent;
        if(parent != null){
        	canvas = parent.getCanvas();
        }else{
        	canvas = null;
        }
        elements = new ArrayList<MenuItem>();
    }

    public void draw(){
        if(subMenu != null){
            subMenu.draw();
            return;
        }
        for(MenuItem e: elements){
            e.draw();
        }
    }
    
    public GrandeCanvas getCanvas(){
    	if(parent == null){
    		return canvas;
    	}else{
    		return parent.getCanvas();
    	}
    }
    
    public void updateMenu(){
    	if(subMenu != null){
    		subMenu.updateMenu();
    	}else{
    		for(MenuItem e: elements){
    			e.update();
            	if(e.contains(canvas.relMousePos)){
            		e.onHover();
            	}else{
            		e.offHover();
            	}
            }
    	}
    }

    public void mousePress(MouseEvent event){
        if(subMenu != null){
            subMenu.mousePress(event);
        }else{
        	for(MenuItem item: elements){
                if(item.contains(event.getX(), event.getY())){
                    item.clicked(event);
                }
            }
        }
    }
    
    public void mouseRelease(MouseEvent event){
        if(subMenu != null){
            subMenu.mouseRelease(event);
        }else{
        	for(MenuItem item: elements){
                item.released(event);
            }
        }
    }

    public void keyPress(int keycode){
    	if(subMenu != null){
    		subMenu.keyPress(keycode);
    	}
    }
}
